package com.example.geta;

import cn.hutool.core.date.DateUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;

public class Sis {



    String host = "https://666.1huisuo.net//forum/";


    @Test
    public void all() {
        //2022年3月23日 20:30:05
        getPicDownUrlFromAllPicPage(TypeEnum.C25, 1, 50, "露出");
        getPicDownUrlFromAllPicPage(TypeEnum.C143, 1, 50, "露出");
        getPicDownUrlFromAllPicPage(TypeEnum.C230, 1, 50, "露出");
        getPicDownUrlFromAllPicPage(TypeEnum.C58, 1, 50, "露出");
        getPicDownUrlFromAllPicPage(TypeEnum.C229, 1, 50, "露出");
    }

    @Test
    public void test() throws IOException {
        DownloadUtil downloadUtil = new DownloadUtil();

//        Document doc = Jsoup.connect("http://23.225.255.96/forum/thread-11186871-1-3.html").get();
//
//        for (Element element : doc.select(".t_msgfont font img")) {
//            String src = element.attr("src");
//            System.out.println(src);
//            String s = "https://qpic.ws/images/2021/10/21/97mk8.jpg";
//            String s1 = FileUtil.extName(s);
//            HttpUtil.downloadFile(s,)
//        }
        String s = "https://csdnimg.cn/release/blogv2/dist/pc/img/npsFeel4.png";
        String s1 = s.substring(s.lastIndexOf("/") + 1);
        File file = new File("C:\\Users\\Administrator\\Desktop\\a", s1);
        downloadUtil.time();
        downloadUtil.download(s,file);

        System.out.println(s1);


    }

    public void getPicDownUrlFromAllPicPage(TypeEnum type, int startPage, int endPage, String key) {
        StringBuilder sb = new StringBuilder();
        for (int i = startPage; i < endPage; i++) {
            System.out.println("正在读取第 " + i + " 页");
            //保存图片的下载地址
            try {
                Document doc = Jsoup.connect(host + "forum-" + type.getCode() + "-" + i + ".html").get();
                for (Element element : doc.select(".lock span a")) {
                    String html = element.html();
                    if (html.contains(key)) {
                        System.out.println("==>读取" + element);
                        String href = element.attr("href");
                        // 详情页
                        Document doc1 = Jsoup.connect(host + href).get();
                        String href1 = doc1.select(".t_attachlist dt a").get(1).attr("href");
                        Document doc2 = Jsoup.connect(host + href1).get();
                        sb.append("<div>")
                                .append("<a href='").append(host).append(doc2.select("#downloadBtn").attr("href")).append("' >")
                                .append(doc1.select(".t_attachlist dd p").html())
                                .append("</a>")
                                .append("====><a href='").append(host).append(href).append("' target='_blank'>").append(html)
                                .append("</a>")
                                .append("</div>");


                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + type + ".html";
        try {
            Files.write(Paths.get("D:\\a", fileName), sb.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getPicDownUrlFromAllPicPage2(TypeEnum type, int startPage, int endPage, String key) {
        StringBuilder sb = new StringBuilder();
        for (int i = startPage; i < endPage; i++) {
            //保存图片的下载地址
            try {
                String host2 = host + "forumdisplay.php?fid=229&filter=type&typeid=653&page=" + i;
                System.out.println("正在读取第 " + i + "," + host2 + " 页");

                Document doc = Jsoup.connect(host2).get();
                for (Element element : doc.select(".lock span a")) {
                    String html = element.html();
                    if (html.contains(key)) {
                        System.out.println("==>读取" + element);
                        String href = element.attr("href");
                        // 详情页
                        Document doc1 = Jsoup.connect(host + href).get();
                        String href1 = doc1.select(".t_attachlist dt a").get(1).attr("href");
                        Document doc2 = Jsoup.connect(host + href1).get();
                        sb.append("<div>")
                                .append("<a href='").append(host).append(doc2.select("#downloadBtn").attr("href")).append("' >")
                                .append(doc1.select(".t_attachlist dd p").html())
                                .append("</a>")
                                .append("====><a href='").append(host).append(href).append("' target='_blank'>").append(html)
                                .append("</a>")
                                .append("</div>");


                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + type + ".html";
        try {
            Files.write(Paths.get("C:\\Users\\Administrator\\Desktop\\a", fileName), sb.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
